/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author jmj
 */
public class SessionManager {

    private final SessionFactory sessionFactory;
    Logger logger = Logger.getLogger("systole");
    static private SessionManager instance = null;

    private SessionManager() {
        try {

            // Create the SessionFactory from hibernate.cfg.xml
            this.sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

        } catch (Throwable ex) {

            // Make sure you log the exception, as it might be swallowed

            System.err.println("Initial SessionFactory creation failed." + ex);
            ex.printStackTrace();
            throw new ExceptionInInitializerError(ex);

        }
    }

    /**
     * @return the unique instance of FacadeDB.
     */
    public static SessionManager getInstance() {

        if (instance == null) {
            instance = new SessionManager();
        }
        return instance;
    }

    	
    /**
     * @return A new DB session.
     */
    public synchronized Session getNewSession(){
        return  this.sessionFactory.openSession();
    }
}
